home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0192.ARJ / L3.C < prev    next >
Text File  |  1991-09-25  |  3KB  |  54 lines

  1. /* Transforms convex polygon Poly (which has PolyLength vertices),
  2.    performing the transformation according to Xform (which generally
  3.    represents a transformation from object space through world space
  4.    to view space), then projects the transformed polygon onto the
  5.    screen and draws it in color Color. Also updates the extent of the
  6.    rectangle (EraseRect) that's used to erase the screen later.
  7.    Tested with Borland C++ 2.0 in the small model */
  8. #include "polygon.h"
  9.  
  10. void XformAndProjectPoly(double Xform[4][4], struct Point3 * Poly,
  11.    int PolyLength, int Color)
  12. {
  13.    int i;
  14.    struct Point3 XformedPoly[MAX_POLY_LENGTH];
  15.    struct Point ProjectedPoly[MAX_POLY_LENGTH];
  16.    struct PointListHeader Polygon;
  17.  
  18.    /* Transform to view space, then project to the screen */
  19.    for (i=0; i<PolyLength; i++) {
  20.       /* Transform to view space */
  21.       XformVec(Xform, (double *)&Poly[i], (double *)&XformedPoly[i]);
  22.       /* Project the X & Y coordinates to the screen, rounding to the
  23.          nearest integral coordinates. The Y coordinate is negated to
  24.          flip from view space, where increasing Y is up, to screen
  25.          space, where increasing Y is down. Add in half the screen
  26.          width and height to center on the screen */
  27.       ProjectedPoly[i].X = ((int) (XformedPoly[i].X/XformedPoly[i].Z *
  28.             PROJECTION_RATIO*(SCREEN_WIDTH/2.0)+0.5))+SCREEN_WIDTH/2;
  29.       ProjectedPoly[i].Y = ((int) (XformedPoly[i].Y/XformedPoly[i].Z *
  30.             -1.0 * PROJECTION_RATIO * (SCREEN_WIDTH / 2.0) + 0.5)) +
  31.             SCREEN_HEIGHT/2;
  32.       /* Appropriately adjust the extent of the rectangle used to
  33.          erase this page later */
  34.          if (ProjectedPoly[i].X > EraseRect[NonDisplayedPage].Right)
  35.           if (ProjectedPoly[i].X < SCREEN_WIDTH)
  36.             EraseRect[NonDisplayedPage].Right = ProjectedPoly[i].X;
  37.           else EraseRect[NonDisplayedPage].Right = SCREEN_WIDTH;
  38.          if (ProjectedPoly[i].Y > EraseRect[NonDisplayedPage].Bottom)
  39.           if (ProjectedPoly[i].Y < SCREEN_HEIGHT)
  40.             EraseRect[NonDisplayedPage].Bottom = ProjectedPoly[i].Y;
  41.           else EraseRect[NonDisplayedPage].Bottom = SCREEN_HEIGHT;
  42.          if (ProjectedPoly[i].X < EraseRect[NonDisplayedPage].Left)
  43.           if (ProjectedPoly[i].X > 0)
  44.             EraseRect[NonDisplayedPage].Left = ProjectedPoly[i].X;
  45.           else EraseRect[NonDisplayedPage].Left = 0;
  46.          if (ProjectedPoly[i].Y < EraseRect[NonDisplayedPage].Top)
  47.           if (ProjectedPoly[i].Y > 0)
  48.             EraseRect[NonDisplayedPage].Top = ProjectedPoly[i].Y;
  49.           else EraseRect[NonDisplayedPage].Top = 0;
  50.    }
  51.    /* Draw the polygon */
  52.    DRAW_POLYGON(ProjectedPoly, PolyLength, Color, 0, 0);
  53. }
  54.